var signed2 = {
    init: function() {
        signed2.getData();
    },
    getData: function() {
        var isLogin = $('#isLogin').val();
        if (isLogin != "1") {
            if (window.isWebView) {
                $.startLogin(function(data) {
                    alert(JSON.stringify(data));
                });
            } else {
                alert('未登录！');
                //   location.href = "https://passport.migu.cn/login?sourceid=203005&apptype=2&forceAuthn=false&isPassive=false&authType=&display=&callbackURL="+
                //     location.host + location.pathname
                //   +"&relayState=login"
            }
        }
        if (isLogin == "1") {
            $.ajax({
                url: 'signedData.jsp',
                dataType: 'json',
                type: 'post',
                async: true,
                scriptCharset: 'utf-8',
                success: function(data) {
                    console.log(data);
                    var data1 = data.queryRechargeInfoList; //未激活
                    var data2 = data.paymentInfos; //激活
                    for (var i = 0; i < data1.length; i++) {
                        if (data1[i].expiryDate * 1 >= signed2.getDate() * 1) { //未过期
                            var res1 = '';
                            if (data1[i].resourceId == '15198008121262654684316') { //7天钻石会员
                                res1 = '<li class="prize1_1" data-resourceId="' + data1[i].resourceId + '" data-rechargeNum="' + data1[i].rechargeNum + '" data-expiryDate="' + data1[i].expiryDate + '"><div>' +
                                    '<img class="rewards_img" src="./img/secPage_7dayVip.png" alt=""><p>7天钻石体验会员</p>' +
                                    '<div class="bt"><img class="btn" src="./img/second-level-active.png" alt=""></div></div></li>';
                            } else if (data1[i].resourceId == '15198005668506353835023') { //3天钻石会员
                                res1 = '<li class="prize1_2" data-resourceId="' + data1[i].resourceId + '" data-rechargeNum="' + data1[i].rechargeNum + '" data-expiryDate="' + data1[i].expiryDate + '"><div>' +
                                    '<img class="rewards_img" src="./img/secPage_3dayVip.png" alt=""><p>3天钻石体验会员</p>' +
                                    '<div class="bt"><img class="btn" src="./img/second-level-active.png" alt=""></div></div></li>';
                            } else if (data1[i].resourceId == '15198010248282132408860') {
                                res1 = '<li class="prize1_2" data-resourceId="' + data1[i].resourceId + '" data-rechargeNum="' + data1[i].rechargeNum + '" data-expiryDate="' + data1[i].expiryDate + '"><div>' +
                                    '<img class="rewards_img" src="./img/1g_7days.png" alt=""><p>1G7天流量包</p>' +
                                    '<div class="bt"><img class="btn" src="./img/second-level-active.png" alt=""></div></div></li>';
                            }
                            $('.no-active ul').append(res1);
                        } else if (data1[i].expiryDate * 1 < signed2.getDate() * 1) { //奖品已过期
                            var res = "";
                            if (data1[i].resourceId == '15198008121262654684316') { //7天钻石会员past-due ul').append(res)
                                res = '<li  class="prize3_1"><div class="alcative_img"><img class="past_pic" src="./img/secPage_7dayVip.png" alt=""><p>7天钻石体验会员</p></div></li>';
                            }
                            if (data1[i].resourceId == '15198005668506353835023') { //3天钻石会员
                                res = '<li  class="prize3_1"><div class="alcative_img"><img class="past_pic" src="./img/secPage_3dayVip.png" alt=""><p>3天钻石体验会员</p></div></li>';
                            }
                            if (data1[i].resourceId == '15198010248282132408860') {
                                res = '<li  class="prize3_1"><div class="alcative_img"><img class="past_pic" src="./img/1g_7days.png" alt=""><p>1G7天流量包</p></div></li>';
                            }
                            $('.past-due ul').append(res)
                        }
                    }
                    $(".no-active ul li").click(function() {
                        var resourceId = $(this).attr("data-resourceId");
                        var rechargeNum = $(this).attr("data-rechargeNum");
                        console.log("1")
                        signed2.activation(resourceId, rechargeNum)
                    })
                    for (var j = 0; j < data2.length; j++) { //已经激活奖品
                        var res2 = '';
                        if (data2[j].productDesc == '兑换咪咕视频体验会员卡7天') {
                            res2 = '<li class="prize2_1"><div class="alcative_img"><img src="./img/secPage_7dayVip.png" alt=""> <p>7天钻石体验会员</p> </div></li>';

                        } else if (data2[j].productDesc == '兑换咪咕视频体验会员卡3天') {
                            res2 = '<li class="prize2_1"><div class="alcative_img"><img src="./img/secPage_3dayVip.png" alt=""> <p>3天钻石体验会员</p> </div></li>';
                        } else if (data2[j].productDesc == '兑换1G7天流量包-广东') {
                            res2 = '<li class="prize2_1"><div class="alcative_img"><img src="./img/1g_7days.png" alt=""> <p>1G7天流量包</p> </div></li>';
                        }
                        $('.jihuo ul').append(res2);
                    }
                  if ($('.no-active ul').children('li').length > 0)  {
                       $('.no_ac_img').css('display', 'none');
                     }
                  if ($('.past-due ul').children('li').length > 0) {
                     $('.past_ac_img').css('display', 'none');
                      }
                   if ($('.jihuo ul').children('li').length > 0)  {
                     $('.al_ac_img').css('display', 'none');
                    }
                },
                error: function(data, a, b) {
                    console.log(data, a, b);
                }
            });
        } else {
            alert('您还未登录！');
        }
    },
    getDate: function() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        if (month >= 0 && month <= 9) {
            month = "0" + month;
        }
        if (day >= 0 && day <= 9) {
            day = "0" + day;
        }
        if (hour >= 0 && hour <= 9) {
            hour = "0" + hour;
        }
        if (minutes >= 0 && minutes <= 9) {
            minutes = "0" + minutes;
        }
        if (seconds >= 0 && seconds <= 9) {
            seconds = "0" + seconds;
        }
        var currentdate = year + month + day + hour + minutes + seconds
        return currentdate;
    },
    activation: function(resourceId, rechargeNum) {
        var signkey = "",
            signKey = "";

        if (resourceId == "15198008121262654684316") { //7天钻石会员
            intfId = "15223222095618286620916325174560";
            signKey = "19a99c3f-7bbe-425e-a301-b5479d9d53f9";
            $('.pop .cont').html('7天咪咕视频钻石体验会员已经激活，请尽快使用~');
        } else if (resourceId == "15198005668506353835023") { //3天钻石会员
            intfId = "15223219760014990134487820736285";
            signKey = "3d8ebb08-f892-443a-a9da-43349bb6ce0d";
            $('.pop .cont').html('3天咪咕视频钻石体验会员已经激活，请尽快使用~');
        } else if (resourceId == "15198010248282132408860") { //1G流量
            intfId = "15223222450729678167169789353255";
            signKey = "3f3780e6-f4ae-4eed-b252-f6f05d40bfd8";
            $('.pop .cont').html('1G7天流量包流量已经激活，请尽快使用~');
        };
        var _this = this;
        var imei = 10000,
            idfa = "",
            res = "",
            sign = "";
        var s = {
            "mobile": $("#mobile").val(),
            "resourceId": resourceId,
            "rechargeNum": rechargeNum
        };
        $.ajax({
            url: "/promactivity/queryAct/getToken",
            type: "POST",
            dataType: "JSON",
            async: true,
            data: {
                intfId: intfId,
                signKey: signKey,
                isDefault: 0
            },
            beforeSend: function(jqXHR) {},
            success: function(data, status, jqXHR) {
                var code = data.code;
                if (code == "1014") {
                    location.href = "https://passport.migu.cn/login?sourceid=203004&apptype=2&forceAuthn=true&isPassive=false&authType=&display=&callbackURL=" + window.location.href + "&relayState=login";
                } else {
                    res = "userId=" + data.userId + "&intfId=" + intfId + "&imei=" + imei + "&idfa=" + idfa + "&token=" + data.tag + "&signKey=" + signKey;
                    sign = $.md5(res);
                    console.log(res);
                    console.log(sign)
                    var body = JSON.stringify(s);
                    $.ajax({
                        url: "/promactivity/activity/execute",
                        type: "POST",
                        dataType: "JSON",
                        async: true,
                        data: {
                            intfId: intfId,
                            sign: sign,
                            imei: 10000,
                            from: "CMVIDEO",
                            httpbody: body
                        },
                        beforeSend: function(jqXHR) {},
                        success: function(data1, status, jqXHR) {
                            if (data1.code == 1) {
                                $('.pop').css('display', 'block');
                            } else {
                                alert('激活失败，请稍后再试~');
                            }

                        },
                        error: function(data) {
                            console.log(data);
                        }
                    })
                }
            }
        })
    },
    handClick: function() {
        $('.pop').css('display', 'none');
        location.reload();
    }
};
if ($.getUrlParam("platform") == "android" || $.getUrlParam("platform") == "ios") {
    window.isWebView = true;
}
if (window.isWebView) {
    window.mnWebMain.init(function() {
        signed2.init();
    })
} else {
    signed2.init();
}